<?php

class baseUserModel extends CDBModelBase {
	public function getTabelName(){
		return "tbl_base_user";
	}

	public function getPKID(){
		return "USER_ID";
	}
	
	protected function getSequence()
	{
		return "BASE_USER_SEQUENCE";
	}
	
	protected $_dateColumns="last_login_time";

	/**
	* 修改
	* @param int(11) $modelInfo["user_id"] 管理员id
	* @param varchar(32) $modelInfo["password"] 管理员密码
	* @param varchar(32) $modelInfo["realname"] 真名
	* @param datetime $modelInfo["update_time"] 更新时间
	* @return int 返回影响行数
	*/
	public function update($modelInfo){
		
		if(!$modelInfo["password"])
		{
			unset($modelInfo["password"]);
		}
		
		return $this->updateByPkId($modelInfo);
	}
	
	/**
	 * 删除
	 * @param int $userId
	 */
	public function delete($userId)
	{
		$this->deleteByPkId($userId);
		
		$params = array(array(":user_id",$userId,PDO::PARAM_INT));
		
		$this->db->executeNonQuery("delete from tbl_base_user_role where user_id=:user_id",$params);
	}
    
    /**
     * 检查用户名是否已存在
     * @param string $username
     * @return bool 返回是否存在,true表示已经存在
     */
    public function checkUserName($username)
    {
        $params = array(array(":username",$username,PDO::PARAM_STR));
		$count = $this->db->executeScalar(
				"select count(*) from ".$this->getTabelName()." where username=:username and del_flag=0",$params);
		
		return $count>0?true:false;
    }

	/**
	 * 用户登录
	 * @param string $userName 用户名
	 * @param string $pwd 密码
	 */
	public function userLogin($userName, $pwd)
	{
		$param = array(
			array(":username",$userName,PDO::PARAM_STR),
			array(":password",$pwd,PDO::PARAM_STR)
		);
		
		return $this->db->executeFetch("select user_id,username,realname from ".$this->getTabelName()." where del_flag=0 and username=:username and password=:password",$param);
	}

	/**
	 * 登录成功后保存登录次数和最后登录时间
	 * @param unknown_type $userID
	 */
	public function afterLogin($userID)
	{
		$params = $this->getParams(array("user_id"=>$userID,"last_login_time"=>$this->getSysDate()));				

		return $this->db->executeNonQuery("update ".$this->getTabelName()." set login_count=login_count+1,last_login_time=to_date(:last_login_time,'yyyy/mm/dd hh24:mi:ss') where user_id=:user_id",$params);
	}
	

	/**
	 * 查询用户页面权限
	 * @param int $userId
	 */
	public function selectUserPageRight($userId)
	{
		$params = array(array(":user_id", $userId,PDO::PARAM_INT));
		return $this->db->executeDataSet("select * from (
			select m.module_name,m.identity_name,m.url,rm.right_value,mr.right_value as rv,mr.right_name,mr.right_ename 
			from tbl_base_user u 
			inner join tbl_base_user_role ur on ur.user_id=u.user_id and u.user_id=:user_id and u.state=1 and u.del_flag=0
			inner join tbl_base_role_module rm on ur.role_id=rm.role_id
			inner join tbl_base_module m on m.module_id=rm.module_id and m.del_flag=0
			inner join tbl_base_module_right mr on mr.module_id=rm.module_id) umr
			where BITAND(umr.rv,umr.right_value)>0",$params);
	}
	
	
	/**
	 * 得到用户模块权限值
	 * @param int $userId
	 */
	public function selectUserModuleRightVaule($userId)
	{
		$params = array(array(":user_id", $userId,PDO::PARAM_INT));
		return $this->db->executeDataSet("select DISTINCT rm.right_value,m.module_name,m.identity_name,m.url,m.sort_num as module_sort
		  ,m.is_show,m.module_thumb,mg.group_name,mg.group_level,mg.sort_num as group_sort,m.modulelevel from 
	      tbl_base_user u
	      inner join tbl_base_user_role ur on ur.user_id=u.user_id
	      inner join tbl_base_role_module rm on ur.role_id=rm.role_id and rm.right_value>0
	      inner join tbl_base_module m on m.module_id=rm.module_id and m.del_flag=0 and m.module_state=1
	      inner join tbl_base_module_group mg on m.module_group_id=mg.module_group_id
	       where u.user_id=:user_id and u.state=1 and u.del_flag=0
	      order by group_sort,module_sort",$params);
	}
	
	/**
	 * 增加一条用户和社区对应关系
	 * @param int $userID
	 * @param int $communityID
	 */
	public function addUserCommunity($userID, $communityID)
	{
		$param = array(
			array(":uc_id",$this->getSequenceNextVal(),PDO::PARAM_INT),
			array(":user_id",$userID,PDO::PARAM_INT),
			array(":community_id",$communityID,PDO::PARAM_INT)
		);
		
		return $this->db->executeNonQuery("
		insert into TBL_BASE_USER_COMMUNITY(uc_id,user_id,community_id)
		values(:uc_id, :user_id, :community_id)"
		,$param);
	}
	
	/**
	 * 删除用户有权限管理的社区
	 * @param int $userID
	 */
	public function delUserCommunity($userID)
	{
		$param = array(
			array(":user_id",$userID,PDO::PARAM_INT)
		);
		
		return $this->db->executeNonQuery("
		delete from TBL_BASE_USER_COMMUNITY
		where user_id=:user_id"
		,$param);
	}
	
	/**
	 * 获取用户有权限管理的社区
	 * @param int $userID
	 */
	public function getUserCommunity($userID)
	{
		$param = array(
			array(":user_id",$userID,PDO::PARAM_INT)
		);
		
		return $this->db->executeDataSet("
		select uc.user_id,uc.community_id,c.community_name from TBL_BASE_USER_COMMUNITY uc, t_community c
		where uc.community_id=c.community_id and user_id=:user_id"
		,$param);
	}
}
?>